package com.gf.help.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.help.dao.ReplyDao;
import com.gf.help.mapper.ReplyMapper;
import com.gf.help.model.Reply;
@Repository("replyDao")
public class ReplyDaoImpl extends BaseDaoImpl implements ReplyDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Value("${replyDao.queryRepliesByQuestionIdSql}")
	private String queryRepliesByQuestionIdSql;
	@Value("${replyDao.queryCountByQuestionIdSql}")
	private String queryCountByQuestionIdSql;
	@Value("${replyDao.insertSql}")
	private String insertSql;
	@Value("${replyDao.deleteSql}")
	private String deleteSql;
	@Value("${replyDao.updateSql}")
	private String updateSql;
	@Value("${replyDao.deleteByQuestionIdSql}")
	private String deleteByQuestionIdSql;
	@Override
	public List<Reply> queryRepliesByQuestionId(long questionId, int pageSize,
			int currentPage) {
		List<Reply> replies = jdbcTemplate.query(queryRepliesByQuestionIdSql,
				ReplyMapper.getMapper(), questionId, (currentPage - 1)
						* pageSize, pageSize);
		return replies;
	}

	@Override
	public int queryCountByQuestionId(long questionId) {
		int count = jdbcTemplate.queryForInt(queryCountByQuestionIdSql,
				questionId);
		return count;
	}

	@Override
	public boolean insert(long replierId,String replierName,long questionId,String content,String replierPortrait) {
		long result=this.insertWithIdReturn(insertSql,replierName,replierId,content,replierPortrait,questionId);
		if(result<=0){
		return false;
		}
		return true;
	}

	@Override
	public boolean delete(long id) {
		int i=jdbcTemplate.update(deleteSql,id);
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean update(String content,long id) {
		int i=jdbcTemplate.update(updateSql,content,id);
		if (i != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean deleteByQuestionId(long questionId) {
		int i=jdbcTemplate.update(deleteByQuestionIdSql,questionId);
		if (i != 0) {
			return true;
		}
		return false;
	}

}
